<?php
// 应用公共文件

//获取ip
function get_client_ip(){
    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
        $ip = getenv("HTTP_CLIENT_IP");
    else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
        $ip = getenv("REMOTE_ADDR");
    else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
        $ip = $_SERVER['REMOTE_ADDR'];
    else
        $ip = "unknown";
    return $ip;
}

/**
 * 数据签名认证
 * @param  array  $data 被认证的数据
 * @return string       签名
 */
function data_auth_sign($data) {
    //数据类型检测
    if(!is_array($data)){
        $data = (array)$data;
    }
    ksort($data); //排序
    $code = http_build_query($data); //url编码并生成query字符串
    $sign = sha1($code); //生成签名
    return $sign;
}

function json_msg($data = [])
{
    print_r(json_encode($data,JSON_UNESCAPED_UNICODE));
    die;
}

//判断文件夹是否存在不存在则创建
function mkdirs_folder($dir, $mode = 0777){
    if (is_dir($dir) || @mkdir($dir, $mode)) return TRUE;
    if (!mkdirs_folder(dirname($dir), $mode)) return FALSE;
    return @mkdir($dir, $mode);
}
function setLogHour($name,$content){
    $ary = explode("_",$name);
    if(count($ary)>1){
        $folder = $ary[0];
    }else{
        $folder = 'public';
    }

    $fileName = $name.'_'.date('Y-m-d_H').'.txt';	//日志文件
    $catalogue = './log';						//日志公共目录
    mkdirs_folder($catalogue);
    $filePath = $catalogue.'/'.$folder;				//日志类别目录
    mkdirs_folder($filePath);
    $fileUrl = $filePath.'/'.$fileName;				//完整路径
    $Ts = fopen($fileUrl,"a+");
    fputs($Ts,"\r\n"."执行日期：".date('Y-m-d H:i:s',time()).  ' ' . "\n" .$content."\n");
    fclose($Ts);
}